package com.samples.servlet;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.samples.model.Customer;
import com.samples.model.DbHelper;

/**
 *
 * @author Bora SAYINER
 * @since Jan 24, 2015 7:15:53 PM
 * @version 1.0
 */
@WebServlet(urlPatterns = { "/CrudServlet" })
public class CrudServlet extends HttpServlet {

	private final DbHelper dbHelper;

	public CrudServlet() {
		this.dbHelper = new DbHelper();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Gson gson = new Gson();
		String action = req.getParameter("action");
		String result = null;
		switch (action) {
			case "list":
				result = generateListResponse(req, gson);
				break;
			case "create":
				result = generateCreateResponse(req, gson);
				break;
			case "update":
				result = generateUpdateResponse(req);
				break;
			case "delete":
				result = generateDeleteResponse(req);
				break;
		}
		resp.setContentType("text/json");
		resp.getWriter().print(result);
	}

	private String generateUpdateResponse(HttpServletRequest req) {
		Enumeration<String> parameterNames = req.getParameterNames();
		while (parameterNames.hasMoreElements()) {
			String name = parameterNames.nextElement();
			String value = req.getParameter(name);
			System.out.println(name + " " + value);
		}
		String result;
		int id = Integer.valueOf(req.getParameter("id"));
		String name = req.getParameter("name");
		String email = req.getParameter("email");
		String phone = req.getParameter("phone");
		String city = req.getParameter("city");
		Customer customer = new Customer(name, email, phone, city);
		customer.setId(id);
		dbHelper.updateCustomer(customer);
		result = "{\"Result\":\"OK\"}";
		return result;
	}

	private String generateDeleteResponse(HttpServletRequest req) {
		String result;
		int id = Integer.valueOf(req.getParameter("id"));
		dbHelper.deleteCustomer(id);
		result = "{\"Result\":\"OK\"}";
		return result;
	}

	private String generateCreateResponse(HttpServletRequest req, Gson gson) {
		String result;
		String name = req.getParameter("name");
		String email = req.getParameter("email");
		String phone = req.getParameter("phone");
		String city = req.getParameter("city");
		Customer customer = new Customer(name, email, phone, city);
		dbHelper.insertCustomer(customer);
		result = "{\"Result\":\"OK\",\"Record\":" + gson.toJson(customer) + "}";
		return result;
	}

	private String generateListResponse(HttpServletRequest req, Gson gson) {
		int startIndex = Integer.valueOf(req.getParameter("jtStartIndex"));
		int pageSize = Integer.valueOf(req.getParameter("jtPageSize"));
		String result;
		List<Customer> customers = dbHelper.getCustomerByPage(startIndex, pageSize);
		result = "{\"Result\":\"OK\",\"Records\":" + gson.toJson(customers) + ",\"TotalRecordCount\":" + dbHelper.getAll().size() + "}";
		return result;
	}

}
